package agents.election;

import agents.TrafficLightAgent;

public class StateBallot {

	private static int seq = 0;
	private static final Object commonLock = new Object();
	private static int winner = 0;
	private static final Object winnerLock = new Object();
	
	private final TrafficLightAgent agent;
	

	private final float vote;
	private final long waitTime;
	private final int hash;
	private final long winnerCount;
	
	public StateBallot(TrafficLightAgent agent, float vote, long waitTime) {
		this.agent = agent;
		this.vote = vote;
		this.waitTime = waitTime;
		this.winnerCount = 0;
		
		synchronized (commonLock) {
			hash = StateBallot.seq++;
		}
	}
	
	private StateBallot(TrafficLightAgent agent, long waitTime) {
		this.agent = agent;
		this.vote = 0;
		this.waitTime = waitTime;
		this.hash = 0;
		
		synchronized (winnerLock) {
			winnerCount = ++StateBallot.winner;
		}
	}
	
	public final TrafficLightAgent getAgent() {
		return agent;
	}

	public final float getVote() {
		return vote;
	}

	public final long getWaitTime() {
		return waitTime;
	}

	public final long getWinnerCount() {
		return winnerCount;
	}
	
	public static StateBallot getWinnerBallot(TrafficLightAgent agent, long waitTime) {
		return new StateBallot(agent, waitTime);
	}
	
	@Override
	public int hashCode() {
		return hash;
	}
}
